==== New external libraries ====
* Added wikimedia/password-blacklist 0.1.4.
* …
+* Added guzzlehttp/guzzle 6.3.3 and dependents:
+ * guzzlehttp/promises 1.3.1
+ * guzzlehttp/psr7 1.5.0
+ * psr/http-message 1.0.1
+ * ralouphie/getallheaders 2.0.5
==== Changed external libraries ====
* Updated wikimedia/xmp-reader from 0.6.0 to 0.6.1.
'GitInfo' => __DIR__ . '/includes/GitInfo.php',
'GlobalDependency' => __DIR__ . '/includes/cache/CacheDependency.php',
'GlobalVarConfig' => __DIR__ . '/includes/config/GlobalVarConfig.php',
+ 'GuzzleHttpRequest' => __DIR__ . '/includes/http/GuzzleHttpRequest.php',
'HHVMMakeRepo' => __DIR__ . '/maintenance/hhvm/makeRepo.php',
'HTMLApiField' => __DIR__ . '/includes/htmlform/fields/HTMLApiField.php',
'HTMLAutoCompleteSelectField' => __DIR__ . '/includes/htmlform/fields/HTMLAutoCompleteSelectField.php',
"ext-json": "*",
"ext-mbstring": "*",
"ext-xml": "*",
+ "guzzlehttp/guzzle": "6.3.3",
+ "guzzlehttp/promises": "1.3.1",
+ "guzzlehttp/psr7": "1.5.0",
"liuggio/statsd-php-client": "1.0.18",
"oojs/oojs-ui": "0.29.6",
"pear/mail": "1.4.1",
"pear/mail_mime": "1.10.2",
"pear/net_smtp": "1.8.0",
"php": ">=5.6.99",
+ "psr/http-message": "1.0.1",
"psr/log": "1.0.2",
+ "ralouphie/getallheaders": "2.0.5",
"wikimedia/assert": "0.2.2",
"wikimedia/at-ease": "1.2.0",
"wikimedia/base-convert": "2.0.0",
return $status;
}
- // If it is a file, move it first.
- // It is done before all other moving stuff is done because it's hard to revert.
$dbw = wfGetDB( DB_MASTER );
- if ( $this->oldTitle->getNamespace() == NS_FILE ) {
- $file = wfLocalFile( $this->oldTitle );
- $file->load( File::READ_LATEST );
- if ( $file->exists() ) {
- $status = $file->move( $this->newTitle );
- if ( !$status->isOK() ) {
- return $status;
- }
- }
- // Clear RepoGroup process cache
- RepoGroup::singleton()->clearCache( $this->oldTitle );
- RepoGroup::singleton()->clearCache( $this->newTitle ); # clear false negative cache
- }
-
$dbw->startAtomic( __METHOD__ );
Hooks::run( 'TitleMoveStarting', [ $this->oldTitle, $this->newTitle, $user ] );
$store->duplicateAllAssociatedEntries( $this->oldTitle, $this->newTitle );
}
+ // If it is a file then move it last.
+ // This is done after all database changes so that file system errors cancel the transaction.
+ if ( $this->oldTitle->getNamespace() == NS_FILE ) {
+ $status = $this->moveFile( $this->oldTitle, $this->newTitle );
+ if ( !$status->isOK() ) {
+ $dbw->cancelAtomic( __METHOD__ );
+ return $status;
+ }
+ }
+
Hooks::run(
'TitleMoveCompleting',
[ $this->oldTitle, $this->newTitle,
return Status::newGood();
}
+ /**
+ * Move a file associated with a page to a new location.
+ * Can also be used to revert after a DB failure.
+ *
+ * @access private
+ * @param Title Old location to move the file from.
+ * @param Title New location to move the file to.
+ * @return Status
+ */
+ private function moveFile( $oldTitle, $newTitle ) {
+ $status = Status::newFatal(
+ 'cannotdelete',
+ $oldTitle->getPrefixedText()->escaped()
+ );
+
+ $file = wfLocalFile( $oldTitle );
+ $file->load( File::READ_LATEST );
+ if ( $file->exists() ) {
+ $status = $file->move( $newTitle );
+ }
+
+ // Clear RepoGroup process cache
+ RepoGroup::singleton()->clearCache( $oldTitle );
+ RepoGroup::singleton()->clearCache( $newTitle ); # clear false negative cache
+ return $status;
+ }
+
/**
* Move page to a title which is either a redirect to the
* source page or nonexistent
--- /dev/null
+<?php
+/**
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
+ * @file
+ */
+
+use GuzzleHttp\Client;
+use GuzzleHttp\Psr7\Request;
+
+/**
+ * MWHttpRequest implemented using the Guzzle library
+ *
+ * Differences from the CurlHttpRequest implementation:
+ * 1) the MWHttpRequest 'callback" option is unsupported. Instead, use the 'sink' option to
+ * send a filename/stream (see http://docs.guzzlephp.org/en/stable/request-options.html#sink)
+ * 2) callers may set a custom handler via the 'handler' option.
+ * If this is not set, Guzzle will use curl (if available) or PHP streams (otherwise)
+ * 3) setting either sslVerifyHost or sslVerifyCert will enable both. Guzzle does not allow
+ * them to be set separately.
+ *
+ * @since 1.33
+ */
+class GuzzleHttpRequest extends MWHttpRequest {
+ const SUPPORTS_FILE_POSTS = true;
+
+ protected $handler = null;
+ protected $sink = null;
+ protected $guzzleOptions = [ 'http_errors' => false ];
+
+ /**
+ * @param string $url Url to use. If protocol-relative, will be expanded to an http:// URL
+ * @param array $options (optional) extra params to pass (see Http::request())
+ * @param string $caller The method making this request, for profiling
+ * @param Profiler|null $profiler An instance of the profiler for profiling, or null
+ * @throws Exception
+ */
+ public function __construct(
+ $url, array $options = [], $caller = __METHOD__, $profiler = null
+ ) {
+ parent::__construct( $url, $options, $caller, $profiler );
+
+ if ( isset( $options['handler'] ) ) {
+ $this->handler = $options['handler'];
+ }
+ if ( isset( $options['sink'] ) ) {
+ $this->sink = $options['sink'];
+ }
+ }
+
+ /**
+ * @see MWHttpRequest::execute
+ *
+ * @return Status
+ */
+ public function execute() {
+ $this->prepare();
+
+ if ( !$this->status->isOK() ) {
+ return Status::wrap( $this->status ); // TODO B/C; move this to callers
+ }
+
+ if ( $this->proxy ) {
+ $this->guzzleOptions['proxy'] = $this->proxy;
+ }
+
+ $this->guzzleOptions['timeout'] = $this->timeout;
+ $this->guzzleOptions['connect_timeout'] = $this->connectTimeout;
+ $this->guzzleOptions['version'] = '1.1';
+
+ if ( !$this->followRedirects ) {
+ $this->guzzleOptions['allow_redirects'] = false;
+ } else {
+ $this->guzzleOptions['allow_redirects'] = [
+ 'max' => $this->maxRedirects
+ ];
+ }
+
+ if ( $this->method == 'POST' ) {
+ $postData = $this->postData;
+ $this->guzzleOptions['body'] = $postData;
+
+ // Suppress 'Expect: 100-continue' header, as some servers
+ // will reject it with a 417 and Curl won't auto retry
+ // with HTTP 1.0 fallback
+ $this->guzzleOptions['expect'] = false;
+ }
+
+ $this->guzzleOptions['headers'] = $this->reqHeaders;
+
+ if ( $this->handler ) {
+ $this->guzzleOptions['handler'] = $this->handler;
+ }
+
+ if ( $this->sink ) {
+ $this->guzzleOptions['sink'] = $this->sink;
+ }
+
+ if ( $this->caInfo ) {
+ $this->guzzleOptions['verify'] = $this->caInfo;
+ } elseif ( !$this->sslVerifyHost && !$this->sslVerifyCert ) {
+ $this->guzzleOptions['verify'] = false;
+ }
+
+ try {
+ $client = new Client( $this->guzzleOptions );
+ $request = new Request( $this->method, $this->url );
+ $response = $client->send( $request );
+ $this->headerList = $response->getHeaders();
+ $this->content = $response->getBody()->getContents();
+
+ $this->respVersion = $response->getProtocolVersion();
+ $this->respStatus = $response->getStatusCode() . ' ' . $response->getReasonPhrase();
+
+ } catch ( GuzzleHttp\Exception\ConnectException $e ) {
+ // ConnectException is thrown for several reasons besides generic "timeout":
+ // Connection refused
+ // couldn't connect to host
+ // connection attempt failed
+ // Could not resolve IPv4 address for host
+ // Could not resolve IPv6 address for host
+ if ( $this->usingCurl() ) {
+ $handlerContext = $e->getHandlerContext();
+ if ( $handlerContext['errno'] == CURLE_OPERATION_TIMEOUTED ) {
+ $this->status->fatal( 'http-timed-out', $this->url );
+ } else {
+ $this->status->fatal( 'http-curl-error', $handlerContext['error'] );
+ }
+ } else {
+ $this->status->fatal( 'http-request-error' );
+ }
+ } catch ( GuzzleHttp\Exception\RequestException $e ) {
+ if ( $this->usingCurl() ) {
+ $handlerContext = $e->getHandlerContext();
+ $this->status->fatal( 'http-curl-error', $handlerContext['error'] );
+ } else {
+ // Non-ideal, but the only way to identify connection timeout vs other conditions
+ $needle = 'Connection timed out';
+ if ( strpos( $e->getMessage(), $needle ) !== false ) {
+ $this->status->fatal( 'http-timed-out', $this->url );
+ } else {
+ $this->status->fatal( 'http-request-error' );
+ }
+ }
+ } catch ( GuzzleHttp\Exception\GuzzleException $e ) {
+ $this->status->fatal( 'http-internal-error' );
+ }
+
+ if ( $this->profiler ) {
+ $profileSection = $this->profiler->scopedProfileIn(
+ __METHOD__ . '-' . $this->profileName
+ );
+ }
+
+ if ( $this->profiler ) {
+ $this->profiler->scopedProfileOut( $profileSection );
+ }
+
+ $this->parseHeader();
+ $this->setStatus();
+
+ return Status::wrap( $this->status ); // TODO B/C; move this to callers
+ }
+
+ /**
+ * @return bool
+ */
+ protected function usingCurl() {
+ return ( $this->handler && is_a( $this->handler, 'GuzzleHttp\Handler\CurlHandler' ) ) ||
+ ( !$this->handler && extension_loaded( 'curl' ) );
+ }
+
+ /**
+ * Guzzle provides headers as an array. Reprocess to match our expectations. Guzzle will
+ * have already parsed and removed the status line (in EasyHandle::createResponse)z.
+ */
+ protected function parseHeader() {
+ // Failure without (valid) headers gets a response status of zero
+ if ( !$this->status->isOK() ) {
+ $this->respStatus = '0 Error';
+ }
+
+ foreach ( $this->headerList as $name => $values ) {
+ $this->respHeaders[strtolower( $name )] = $values;
+ }
+
+ $this->parseCookies();
+ }
+}
use MWHttpRequest;
use PhpHttpRequest;
use Profiler;
+use GuzzleHttpRequest;
/**
* Factory creating MWHttpRequest objects.
*/
public function create( $url, array $options = [], $caller = __METHOD__ ) {
if ( !Http::$httpEngine ) {
- Http::$httpEngine = function_exists( 'curl_init' ) ? 'curl' : 'php';
+ Http::$httpEngine = 'guzzle';
} elseif ( Http::$httpEngine == 'curl' && !function_exists( 'curl_init' ) ) {
throw new DomainException( __METHOD__ . ': curl (https://secure.php.net/curl) is not ' .
'installed, but Http::$httpEngine is set to "curl"' );
}
switch ( Http::$httpEngine ) {
+ case 'guzzle':
+ return new GuzzleHttpRequest( $url, $options, $caller, Profiler::instance() );
case 'curl':
return new CurlHttpRequest( $url, $options, $caller, Profiler::instance() );
case 'php':
* @param array $options (optional) extra params to pass (see Http::request())
* @param string $caller The method making this request, for profiling
* @param Profiler|null $profiler An instance of the profiler for profiling, or null
+ * @throws Exception
*/
public function __construct(
$url, array $options = [], $caller = __METHOD__, $profiler = null
/**
* Sets HTTPRequest status member to a fatal value with the error
* message if the returned integer value of the status code was
- * not successful (< 300) or a redirect (>=300 and < 400). (see
- * RFC2616, section 10,
- * http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html for a
- * list of status codes.)
+ * not successful (1-299) or a redirect (300-399).
+ * See RFC2616, section 10, http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
+ * for a list of status codes.
*/
protected function setStatus() {
if ( !$this->respHeaders ) {
$this->parseHeader();
}
- if ( (int)$this->respStatus > 399 ) {
+ if ( ( (int)$this->respStatus > 0 && (int)$this->respStatus < 400 ) ) {
+ $this->status->setResult( true, (int)$this->respStatus );
+ } else {
list( $code, $message ) = explode( " ", $this->respStatus, 2 );
+ $this->status->setResult( false, (int)$this->respStatus );
$this->status->fatal( "http-bad-status", $code, $message );
}
}
$arr['class'] = 'allmessages-customised';
}
- if ( !$isSecond ) {
- $arr['id'] = Sanitizer::escapeIdForAttribute(
- 'msg_' . $this->getLanguage()->lcfirst( $row->am_title )
- );
- }
-
return $arr;
}
setContents: function ( content ) {
return this.each( function () {
var scrollTop = this.scrollTop;
- this.select();
- if ( !document.execCommand( 'insertText', false, content ) ) {
- $( this ).val( content );
- }
+ $( this ).val( content );
// Setting this.value may scroll the textarea, restore the scroll position
this.scrollTop = scrollTop;
} );
return this.each( function () {
var allText, currSelection, startPos, endPos;
- if ( !document.execCommand( 'insertText', false, value ) ) {
- allText = $( this ).textSelection( 'getContents' );
- currSelection = $( this ).textSelection( 'getCaretPosition', { startAndEnd: true } );
- startPos = currSelection[ 0 ];
- endPos = currSelection[ 1 ];
+ allText = $( this ).textSelection( 'getContents' );
+ currSelection = $( this ).textSelection( 'getCaretPosition', { startAndEnd: true } );
+ startPos = currSelection[ 0 ];
+ endPos = currSelection[ 1 ];
- $( this ).textSelection( 'setContents', allText.slice( 0, startPos ) + value +
- allText.slice( endPos ) );
- $( this ).textSelection( 'setSelection', {
- start: startPos,
- end: startPos + value.length
- } );
- }
+ $( this ).textSelection( 'setContents', allText.slice( 0, startPos ) + value +
+ allText.slice( endPos ) );
+ $( this ).textSelection( 'setSelection', {
+ start: startPos,
+ end: startPos + value.length
+ } );
} );
},
<?php
+use GuzzleHttp\Handler\MockHandler;
+use GuzzleHttp\HandlerStack;
+use GuzzleHttp\Psr7\Response;
+
/**
* @group Http
* @group small
$this->assertTrue( defined( $value ), $value . ' not defined' );
}
+
+ /**
+ * No actual request is made herein
+ */
+ public function testGuzzleHttpRequest() {
+ $handler = HandlerStack::create( new MockHandler( [ new Response( 200 ) ] ) );
+ $r = new GuzzleHttpRequest( 'http://www.example.text', [ 'handler' => $handler ] );
+ $r->execute();
+ $this->assertEquals( 200, $r->getStatus() );
+
+ // @TODO: add failure tests (404s and failure to connect)
+ }
}
/**
// returns appropriate tester class here
public static function factory( $url, array $options = null, $caller = __METHOD__ ) {
if ( !Http::$httpEngine ) {
- Http::$httpEngine = function_exists( 'curl_init' ) ? 'curl' : 'php';
+ Http::$httpEngine = 'guzzle';
} elseif ( Http::$httpEngine == 'curl' && !function_exists( 'curl_init' ) ) {
throw new DomainException( __METHOD__ . ': curl (https://secure.php.net/curl) is not ' .
'installed, but Http::$httpEngine is set to "curl"' );
}
switch ( Http::$httpEngine ) {
+ case 'guzzle':
+ return new GuzzleHttpRequestTester( $url, $options, $caller );
case 'curl':
return new CurlHttpRequestTester( $url, $options, $caller );
case 'php':
}
}
+class GuzzleHttpRequestTester extends GuzzleHttpRequest {
+ function setRespHeaders( $name, $value ) {
+ $this->respHeaders[$name] = $value;
+ }
+}
+
class CurlHttpRequestTester extends CurlHttpRequest {
function setRespHeaders( $name, $value ) {
$this->respHeaders[$name] = $value;